/* -+- file  ./kf2/Roster/Resource.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using Habazie.kf2.Core;

namespace Habazie.kf2.Roster
{
	
	public class Resource
	{
		///<summary>Resource name</summary>
		private string m_Name;
		
		///<summary>Resource priority</summary>
		private int m_Priority = 0;
		
		///<summary>Presence</summary>
		private jabber.protocol.client.PresenceType m_Presence
			= jabber.protocol.client.PresenceType.unavailable;
		
		///<summary>Presence show</summary>
		private string m_Show = null;
		
		///<summary>Presence status</summary>
		private string m_Status = null;
		
		///<summary>Short status</summary>
		private string m_ShortStatus = null;
		
		public Resource(string name)
		{
			m_Name = name;
		}
		
		public string Name {
			get { return m_Name; }
			set { m_Name = value; }
		}
		
		public jabber.protocol.client.PresenceType Presence {
			get { return m_Presence; }
			set { m_Presence = value; }
		}
		
		public string Show {
			get { return m_Show; }
			set { m_Show = value; }
		}
		
		public string Status {
			get { return m_Status; }
		}
		
		public string ShortStatus {
			get { return m_ShortStatus; }
		}
		
		public int Priority {
			get { return m_Priority; }
		}
		
		public void Update (jabber.protocol.client.Presence p)
		{
		//	m_Name = p.From.Resource;
			try {
				if (p.Priority != null)
					m_Priority = System.Int32.Parse (p.Priority);
			} catch (Exception e) {
				m_Priority = 0;
				Util.ReportException (e);
			}

			jabber.protocol.client.PresenceType old = m_Presence;

			m_Presence = p.Type;
			m_Show = p.Show;
			m_Status = p.Status;

			/* Sound notification*/	
			/* I'm afraid this is a silly place for this */
			Util.Debug ("Roster", "Presence={0}", m_Presence);
			if (m_Presence == jabber.protocol.client.PresenceType.available)
				Util.PlaySound ("online");
			else if (m_Presence == jabber.protocol.client.PresenceType.unavailable)
				Util.PlaySound ("offline");



			if (m_Status == null || m_Status == "") {
				m_ShortStatus = null;
				return;
			}
			
			/* Crop status */
			int nl = m_Status.IndexOf ('\n');
			if (nl == -1)
				nl = m_Status.Length;
			
			int crop = System.Math.Min (nl, 61);
			if (crop > 61 || nl != m_Status.Length)
				m_ShortStatus = m_Status.Substring (0, crop) + "...";
			else
				m_ShortStatus = m_Status;
		}
	}
}
